#ifndef DmxServer_H
#define DmxServer_H

#include "DmxValueList.h"

// Frame Size = NUM_DMX_CHANNELS + 1 byte (first) for frame type (type 0 = send dmx, type 1 = set switch is master, type 2 = set switch on profile, type 3 = notify client switch toggled)
#define DMX_SERVER_FRAME_SIZE NUM_DMX_CHANNELS+1 

#include <QTcpServer>
#include <QThread>
#include <QTcpSocket>

#include "DmxServerFrameTypes.h"

class DmxSetter;

class DmxServer : public QTcpServer
{
	Q_OBJECT

public:
	DmxServer(DmxSetter *setter, QObject *parent = 0);
	
	void start(int port=6159);
	
	DmxSetter *setter() { return m_setter; }
	
protected:
	void incomingConnection(int socketDescriptor);
	
protected slots:
	void clientDisconnected();

private:
	DmxSetter *m_setter;	
};

class DmxServerThread : public QThread
{
	Q_OBJECT

public:
	DmxServerThread(int socketDescriptor, DmxServer *parent);
	~DmxServerThread();

	void run();

signals:
	void error(QTcpSocket::SocketError socketError);
	
	void sendDmx(DmxValueList);
	void setSwitchMater(bool);
	void setSwitchOnProfile(DmxValueList);
	
	void disconnected();

public slots:
	void switchToggled(bool);

private slots:
	void dataReady();
	void processBlock();

private:
	DmxServer *m_server;
	
	QTcpSocket *m_socket;
	int m_socketDescriptor;
	bool m_isConnected;
	QByteArray m_dataBlock;
	
	char m_dataFrame[DMX_SERVER_FRAME_SIZE];
	
};


#endif
